//
//  YMPhotoViewController.m
//  PhotoSelector
//
//  Created by lantaiyuan on 2017/3/1.
//  Copyright © 2017年 洪雯. All rights reserved.
//

#import "YMPhotoViewController.h"
#import "YMAddPhotoViewModel.h"


@interface YMPhotoViewController ()
@property (nonatomic, strong) YMAddPhotoViewModel * viewModel;
@property (nonatomic, strong) UICollectionView * photoCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout * flowLayout;
@property (nonatomic, strong) HWImagePickerSheet *imgPickerActionSheet;
@property (nonatomic, strong) UIView * photoView;
@end

@implementation YMPhotoViewController

- (UIView *)photoView{
    if (!_photoView) {
        _photoView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, Photo_H + 10)];
        _photoView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_photoView];
    }
    return _photoView;
}

- (YMAddPhotoViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[YMAddPhotoViewModel alloc] init];
        [_viewModel initPickerView];
    }
    return _viewModel;
}

- (HWImagePickerSheet *)imgPickerActionSheet{
    if (!_imgPickerActionSheet) {
        _imgPickerActionSheet = [[HWImagePickerSheet alloc] init];
        _imgPickerActionSheet.delegate = self.viewModel;
    }
    return _imgPickerActionSheet;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.photoView addSubview:self.photoCollectionView];
}

- (UICollectionViewFlowLayout *)flowLayout{
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    }
    
    _flowLayout.minimumLineSpacing = 5;
    _flowLayout.minimumInteritemSpacing = 5;
    _flowLayout.itemSize = CGSizeMake(Photo_W, Photo_H);
    
    return _flowLayout;
}

- (UICollectionView *)photoCollectionView{
    if (!_photoCollectionView) {
        _photoCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(5, 0, self.photoView.frame.size.width - 10, Photo_H) collectionViewLayout:self.flowLayout];
        _photoCollectionView.backgroundColor = [UIColor whiteColor];
        _photoCollectionView.dataSource = self.viewModel;
        _photoCollectionView.delegate = self.viewModel;
        self.viewModel.pickerCollectionView = _photoCollectionView;
        
        // Register nib file for the cell
        UINib *nib = [UINib nibWithNibName:@"HWCollectionViewCell" bundle: [NSBundle mainBundle]];
        [_photoCollectionView registerNib:nib forCellWithReuseIdentifier:@"HWCollectionViewCell"];
        
        self.viewModel.imgPickerActionSheet = self.imgPickerActionSheet;
    }
    return _photoCollectionView;
}
@end
